package com.example.day8_sdcard;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.Toast;

/**
 安卓中四大存储：
 1。网络存储：数据在服务器
 2。sp存储：小量数据，5种类型，key/value
 3。sdcard：大文件 持久化存储
 4。sqlite：数据库存储



 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //TODO 1：判断当前版本号
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//23
            //TODO 2:判断权限是否动态授予过
            int code = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
            if(code == PackageManager.PERMISSION_GRANTED){//
                Toast.makeText(this, "授予过", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "没有授予过", Toast.LENGTH_SHORT).show();
                //TODO 3：动态授予权限
                //参数一 ：string数组 参数二：请求码
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
            }
        }



    }
    //TODO 4：处理结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode  == 101){
            int grantResult = grantResults[0];//结果
            if(grantResult ==PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "授予成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "授予失败", Toast.LENGTH_SHORT).show();
            }
        }
    }






}
